package com.shiwaixiangcun.customer.module.housebutler

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.google.gson.reflect.TypeToken
import com.jaeger.recyclerviewdivider.RecyclerViewDivider
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.shiwaixiangcun.customer.ConfigSps
import com.shiwaixiangcun.customer.GlobalApi
import com.shiwaixiangcun.customer.GlobalConfig
import com.shiwaixiangcun.customer.R
import com.shiwaixiangcun.customer.base.BaseActivity
import com.shiwaixiangcun.customer.module.heath.CommonEntity
import com.shiwaixiangcun.customer.network.ResponseConfig
import com.shiwaixiangcun.customer.utils.AppSharePreferenceMgr
import com.shiwaixiangcun.customer.utils.JsonUtil
import com.shiwaixiangcun.customer.widget.CustomLoadMoreView
import kotlinx.android.synthetic.main.activity_service_record.*
import kotlinx.android.synthetic.main.top_bar_white.*

class ServiceRecordActivity : BaseActivity(), View.OnClickListener {

    private var mAdapter: AdapterServiceRecord? = null
    private var mList: MutableList<ServiceRecordEntity>? = null

    private var page = 1
    private var pageSize = 20
    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.back_left -> {
                finish()
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_service_record)
        initViewAndEvent()
        initData(false)
    }

    private fun initData(isLoadMore: Boolean) {
        val strToken = AppSharePreferenceMgr.get(mContext, ConfigSps.CURRENT_TOKEN, "") as String
        OkGo.get<String>(GlobalApi.serviceList)
                .params("access_token", strToken)
                .params("page.pn", page)
                .params("page.size", pageSize)
                .params("fields", "id,content,number,status,createTime")
                .execute(object : StringCallback() {
                    override fun onError(response: Response<String>?) {
                        super.onError(response)
                        mAdapter?.setEnableLoadMore(true)
                        mAdapter?.loadMoreFail()
                        refreshLayout.finishRefresh()
                    }

                    override fun onSuccess(response: Response<String>?) {
                        val type = object : TypeToken<CommonEntity<ServiceRecordList>>() {}.type
                        val responseData = JsonUtil.fromJson<CommonEntity<ServiceRecordList>>(response?.body(), type)
                        when (responseData.responseCode) {
                            ResponseConfig.SUCCESS -> {
                                val size = responseData.data.elements.size
                                if (isLoadMore) {
                                    if (size > 0) {
                                        mList?.addAll(responseData.data.elements)
                                        mAdapter?.notifyDataSetChanged()
                                    }
                                    if (size == 0 || size < pageSize) {
                                        mAdapter?.loadMoreEnd(false)
                                    } else {
                                        mAdapter?.loadMoreComplete()
                                    }
                                } else {
                                    refreshLayout.finishRefresh()
                                    mAdapter?.setEnableLoadMore(true)
                                    if (size == 0) {
                                        rlayout_no_data.visibility = View.VISIBLE
                                    } else {
                                        rlayout_no_data.visibility = View.GONE
                                        mList?.clear()
                                        mList?.addAll(responseData.data.elements)
                                        mAdapter?.notifyDataSetChanged()
                                    }
                                }
                            }
                        }
                    }

                })

    }

    private fun initViewAndEvent() {
        tv_page_name.text = "服务记录"
        back_left.setOnClickListener(this)

        mList = ArrayList()
        mAdapter = AdapterServiceRecord(mList)

        page = GlobalConfig.first_page
        pageSize = GlobalConfig.page_size
        rv_service_record.layoutManager = LinearLayoutManager(this)
        rv_service_record.adapter = mAdapter
        val divider = RecyclerViewDivider.Builder(mContext)
                .setOrientation(RecyclerViewDivider.VERTICAL)
                .setStyle(RecyclerViewDivider.Style.BETWEEN)
                .setMarginLeft(16f)
                .setMarginRight(0f)
                .setSize(0.5f)
                .setEndSkipCount(1)
                .setColorRes(R.color.color_divider_0_1)
                .build()
        rv_service_record.addItemDecoration(divider)
        mAdapter?.setOnItemClickListener { adapter, view, position ->

            val serviceRecordEntity = adapter.data[position] as ServiceRecordEntity
            val bundle = Bundle()
            bundle.putLong("id", serviceRecordEntity.id)
            readyGo(ServiceRecordDetailActivity::class.java, bundle)
        }

        mAdapter?.setLoadMoreView(CustomLoadMoreView())
        mAdapter?.setOnLoadMoreListener({
            page += 1
            initData(true)
            refreshLayout.finishRefresh()
        }, rv_service_record)


        refreshLayout.setOnRefreshListener {
            page = 1
            mAdapter?.setEnableLoadMore(false)
            initData(false)
        }
    }
}
